<!-- 课程管理(模块) => 课程列表(菜单) => 台账操作列(修改任课教师按钮) => 点开后展示此组件 -->
<template>
  <Dialog :title="t('course.newTeacher.title')" v-model="dialogVisible" width="900">
    <!-- 搜索 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('systemUser.fields.nickname')" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.nickname')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-150px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" />
            {{ t('common.search') }}
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" />
            {{ t('common.reset') }}
          </el-button>
          <el-button type="primary" plain @click="handleAddTeacher">
            <Icon icon="ep:plus" />
            {{ t('course.newTeacher.newBtn') }}
          </el-button>
          <el-button type="success" plain @click="handleAddUstcTeacher">
            <Icon icon="ep:user" />
            {{ t('course.newTeacher.ustcBtn') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-alert class="!mb-[10px]" type="error" :closable="false">
        {{ t('course.index.selectTeacherTip') }}
      </el-alert>
      <vxe-table
        id="mentor-select-table"
        ref="tableRef"
        v-loading="loading"
        :data="list"
        stripe
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('systemUser.fields.username')"
          field="username"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('systemUser.fields.nickname')"
          field="nickname"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('systemUser.fields.mobile')"
          field="mobile"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('systemUser.fields.email')"
          field="email"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          show-header-overflow
          show-overflow
          fixed="right"
          width="80"
        >
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button
                type="primary"
                link
                @click="handleChoose(scope.row)"
                :disabled="scope.row.id === course.teacherId"
              >
                <Icon icon="ep:check" />
                {{ t('action.choose') }}
              </el-button>
            </div>
          </template>
        </vxe-column>
      </vxe-table>

      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>

  <AddCourseTeacher ref="addCourseTeacherRef" @success="resetQuery" />
  <AddUstcMentorTeacher ref="addUstcMentorTeacherRef" @choose="ustcChoosed" />
</template>

<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import { UserVO } from '@/api/system/user'
import { useAppStore } from '@/store/modules/app'
import { CourseAdminRespVO, CourseApi } from '@/api/course/course'
import AddCourseTeacher from './AddCourseTeacher.vue'
import AddUstcMentorTeacher from './AddUstcMentorTeacher.vue'

/** 课程分配教师 列表 */
defineOptions({ name: 'CourseTeacherSelect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  roleId: 162, // 162固定为任课教师
  username: undefined,
  nickname: undefined,
  mobile: undefined,
  jobCode: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})

const queryFormRef = ref() // 搜索的表单

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const addCourseTeacherRef = ref<InstanceType<typeof AddCourseTeacher>>()
const handleAddTeacher = () => {
  addCourseTeacherRef.value?.open('create')
}

const addUstcMentorTeacherRef = ref<InstanceType<typeof AddUstcMentorTeacher>>()
const handleAddUstcTeacher = () => {
  addUstcMentorTeacherRef.value?.open(course.value)
}

const handleChoose = async (row: UserVO) => {
  try {
    await message.confirm(t('course.newTeacher.confirm'))
  } catch {
    return
  }
  loading.value = true
  try {
    course.value.teacherId = row.id
    await CourseApi.updateCourseUstcMentor(course.value)
    message.success(t('common.operSuccess'))
    dialogVisible.value = false
    emits('choose')
  } finally {
    loading.value = false
  }
}

const ustcChoosed = () => {
  dialogVisible.value = false
  emits('choose')
}

const course = ref<CourseAdminRespVO>({} as CourseAdminRespVO)
const open = async (row: CourseAdminRespVO) => {
  course.value = row
  dialogVisible.value = true
  // 查询所有有教师角色的人
  getList()
}

const emits = defineEmits(['choose'])

defineExpose({ open })
</script>

<style scoped lang="scss"></style>
